package net.xju.controller;

import net.xju.common.framework.simplemvc.RequestMapping;
import net.xju.common.utils.JSONUtil;
import net.xju.common.utils.Result;
import net.xju.entity.Work;
import net.xju.service.impl.WorkServiceImpl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;

@RequestMapping("/api/work")

public class WorkController {

    WorkServiceImpl workService = new WorkServiceImpl();

    @RequestMapping("/insert.do")
    public void insert(HttpServletRequest request, HttpServletResponse resp) throws UnsupportedEncodingException {

        request.setCharacterEncoding("utf-8");
        Work work = new Work();

        work.setWorkName(request.getParameter("workname"));
        work.setType(request.getParameter("type"));
        work.setCareInfo(request.getParameter("careInfo"));
        work.setUsername(request.getParameter("username"));

        workService.insert(work);
        JSONUtil.printByJSON(resp, Result.success("成功添加"));

    }

    @RequestMapping("/queryByUsername.do")
    public void queryByUsername(HttpServletRequest request, HttpServletResponse resp) throws UnsupportedEncodingException {
        request.setCharacterEncoding("utf-8");
        String username = request.getParameter("username");
        Work work = workService.queryByUsername(username);
        if(work != null){
            JSONUtil.printByJSON(resp, Result.success(work));
        }else{
            JSONUtil.printByJSON(resp,Result.fail("没有查询到当前数据"));
        }

    }

    @RequestMapping("/deleteInFact.do")
    public void deleteInFact(HttpServletRequest request, HttpServletResponse resp) throws UnsupportedEncodingException {
        request.setCharacterEncoding("utf-8");
        String username = request.getParameter("username");
        workService.deleteInFact(username);
        JSONUtil.printByJSON(resp, Result.success("删除成功"));
    }

    @RequestMapping("/updateByUsername.do")
    public void updateByUsername(HttpServletRequest request, HttpServletResponse resp) throws UnsupportedEncodingException {
        request.setCharacterEncoding("utf-8");
        Work work = new Work();
        work.setUsername(request.getParameter("username"));
        work.setWorkName(request.getParameter("workName"));
        work.setType(request.getParameter("type"));
        work.setCareInfo(request.getParameter("careInfo"));

        if(workService.updateByUsername(work)){
            JSONUtil.printByJSON(resp, Result.success("更新成功"));
        }else {
            JSONUtil.printByJSON(resp,Result.fail("更新失败"));
        }
    }

    @RequestMapping("/countWork.do")
    public void countWork(HttpServletRequest request, HttpServletResponse resp) throws UnsupportedEncodingException, SQLException {
        request.setCharacterEncoding("utf-8");
        String  type = request.getParameter("type");
        if(workService.countWork(type)>0){
            JSONUtil.printByJSON(resp,Result.success(workService.countWork(type)));
        }else{
            JSONUtil.printByJSON(resp,Result.fail("此类作品数量当前为0"));
        }
    }
}
